// miniprogram/pages/status/predict/predict.js
var app = getApp()
var wxCharts = require('../../../utils/wxcharts.js')
var predictChart = null
function formatXlabel(a){
  return "第"+a+"次"
}


Page({

  /**
   * 页面的初始数据
   */
  data: {
    index:0,
    array:["1号轴承","2号轴承","3号轴承","4号轴承","5号轴承","6号轴承","7号轴承","8号轴承","9号轴承","10号轴承","11号轴承","12号轴承"],
    filename: [
      "out_2_4188235209205419.csv",
      "out_2_4188236393943714.csv",
      "out_2_4188237608897315.csv",
      "out_2_4188238848566260.csv",
      "out_2_4188240070138485.csv",
      "out_2_4188241280913273.csv",
      "out_2_4188242481625917.csv",
      "out_2_4188243747902208.csv",
      "out_2_4188244966096930.csv",
      "out_2_4188246292398094.csv",
      "out_2_4188247474992582.csv",
      "out_2_4188248745762005.csv"
    ],
    resultArray:{},
    ec: {
      lazyLoad: true
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading({
      title: '正在调用',
    })
    var token = app.globalData.access_token
    var that = this
    wx.request({
      url: 'https://phmlearn.com/component/upload/ML/model/176/399',
      method: 'POST',
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      data: {
        access_token: token,
        file_name: that.data.filename[that.data.index]
      },
      success: function (res) {
        console.log(res.data.data)
        that.setData({
          resultArray:res.data.data.result
        })
        that.getChartdata(res.data.data.predict)
        wx.hideLoading()
      }
    })
    // wx.stopPullDownRefresh()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  getChartdata: function (ydata) {
    var windowWidth = 320;
    try {
      var res = wx.getSystemInfoSync();
      windowWidth = res.windowWidth;
    } catch (e) {
      console.error('getSystemInfoSync failed!');
    }
    predictChart = new wxCharts({ //当月用电折线图配置
      canvasId: 'predict',
      type: 'line',
      categories: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31','32','33','34','35','36','37','38','39','40','41','42','43','44','45','46','47','48','49','50'].map(formatXlabel), //categories X轴
      animation: true,
      series: [{
          name: '轴承损坏程度预测',
          data: ydata,
          format: function (val, name) {
            return val.toFixed(2);
          }
        }
      ],
      xAxis: {
        disableGrid: true
      },
      yAxis: {
        title: '损坏程度',
        format: function (val) {
          return val.toFixed(0);  //y轴标签保留0位小数
        },
        max: 5,
        min: 0
      },
      width: windowWidth,
      height: 200,
      dataLabel: false,
      dataPointShape: true,
      extra: {
        lineStyle: 'curve'
      }
    });
  },
  bindPickerChange: function(e){
    this.setData({
      index: e.detail.value
    })
    this.onLoad()
  }
})